package task.domain.converters
{
    import mx.collections.ArrayList;

    import org.flexthinker.binding.conversion.Converter;

    import task.domain.TaskPriority;

    public class PriorityToIntConverter implements Converter
    {
        private var priorities:ArrayList;

        /**
         *
         * @param prioritiesMetadata
         *          The list with priorities coming from TaskMetadata.
         *          List is used to convert an int into a TaskPriority
         */
        public function PriorityToIntConverter(prioritiesMetadata:ArrayList)
        {
            this.priorities = prioritiesMetadata;
        }


        public function convertForward(value:*):*
        {
            return value ? 5 - TaskPriority(value).priority : 1 ;
        }


        public function convertReverse(value:*):*
        {
            return findTaskByPriority(value);
        }

        private function findTaskByPriority(priority:int):TaskPriority
        {
            for each ( var task:TaskPriority in priorities.source )
            {
                if ( task.priority == 5 - priority )
                {
                    return task;
                }
            }

            return null;
        }
    }
}
